/**
 * Created by Administrator on 2019/7/31.
 */

'use strict';
const requireEx=require('yi-require');
const redis = requireEx('yi-redis');
const validation = require('yi-sms-validat');
const passwordHash = require('yi-password-hash');
const async = require('async');
var objectId = require('mongodb').ObjectId;
/*
* 验证电话和验证码
* */
const  middleware = (req,res,next)=>{
    let ent = req.body;
    let key = "captcha:"+ent.mobile;
    passwordHash.hash(ent.pwd, function (err, result) {
        if(err)return res.err.unknownError(err);
        ent.pwd = result;
        validation(key,ent.verificationCode,function(err,data){
            if (err) return res.err.redisError(err);
            if(!data){
                res.reply({code:1000,message:"验证码错误"});
                return;
            }
            req.mongo.collection("user").find({"mobile":ent.mobile}).toArray(function(err,index){
                if(err){
                    return res.err.errorMongo(err);
                }
                if(index.length>0){
                    res.reply({code:1002,message:"号码已经被注册"});
                }else{
                    return next();
                }
            });
        });
    })

    //redis.get(key,function(err,data){
    //    if (err) return res.err.redisError(err);
    //    if(data==null){
    //        res.reply("验证码错误");
    //        return;
    //    }
    //    if(ent.verificationCode!=JSON.parse(data).randomNum){
    //        res.reply("验证码错误");
    //        return;
    //    }
    //    req.mongo.collection("user").find({"mobile":ent.mobile}).toArray(function(err,index){
    //        if(err){
    //            return res.err.errorMongo(err);
    //        }
    //        if(index.length>0){
    //            res.reply({message:"号码已经被注册"});
    //        }else{
    //            return next();
    //        }
    //    });
    //});
};

/*
* 写入user信息
* */
const handler = (req,res,next)=>{
    let data = req.body;

    async.waterfall([
        function(callback){
            req.mongo.collection("user").insertOne({"mobile":data.mobile,"pwd":data.pwd},(err,result)=>{
                if (err){
                    return callback(err);
                }else{
                    callback(null,result.insertedId);
                }
                //res.reply({code:0,_id:result.insertedId});

            });
        },
        function(result,callback){
            //console.log("key:"+data.mobile);
            //根据手机号获取redis数据，看是否要维护订单参与人
            redis.get(data.mobile,function(err,data){
                if (err){
                    //console.log(1);
                    callback(null,result);
                } else{
                    if(data!=null){
                        let dt = JSON.parse(data);
                        //console.log(2);
                        //console.log(dt.orderId);
                        //console.log(dt.mobile);
                        req.mongo.collection("order").updateOne({_id:objectId(dt.orderId)},{$addToSet:{participants:{partId:result,phone:dt.mobile}}},(err,resultOrder)=>{
                            callback(null,result);
                        });
                    }else{
                        //console.log(3);
                        callback(null,result);
                    }
                }
            });
        }
    ],(err,result)=>{
        if(err)return res.err.mongoError(err);
        res.reply({code:0,_id:result});
    });

};

module.exports={
    open:false,
    description: '登录注册',
    middleware:middleware,
    handler:handler
};

